package net.xi.dmhome.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.xi.dmhome.entity.Answer;
import net.xi.dmhome.entity.Question;
import net.xi.dmhome.handler.Result;
import net.xi.dmhome.mapper.AnswerMapper;
import net.xi.dmhome.mapper.QuestionMapper;
import net.xi.dmhome.service.AnswerService;
import net.xi.dmhome.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: jxhu
 * @Description:咨询栏控制层
 * @Date Created in 2021-02-07 16:17
 * @Modified By:
 */
@RestController
@RequestMapping("/consult")
public class ConsultController {

    @Autowired
    QuestionMapper questionMapper;
    @Autowired
    QuestionService questionService;
    @Autowired
    AnswerMapper answerMapper;
    @Autowired
    AnswerService answerService;

    /* @Description: 问题列表,@Param:  */
    @GetMapping("/listQ")
    public Result listQuestion(@RequestParam(defaultValue = "1") Integer currentPage){
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ispast", 1).orderByDesc("createtime");
        Page page = new Page(currentPage,5);
        IPage iPage = questionService.page(page, queryWrapper);

        return Result.success(iPage);

    }

    /* @Description: 问题信息添加,@Param:  */
    @PostMapping("/addQ")
    public Result addQ(@Validated @RequestBody(required = false) Question question) throws Exception {

        int insert = questionMapper.insert(question);
        if (insert != 0) {

            System.out.println("添加成功");
            return Result.success(200, "success", insert);
        } else {
            return Result.fail("添加失败");
        }
    }

    /* @Description: 根据id查找问题,@Param:  */
    @GetMapping("/findQbyid/{id}")
    public Result getQuestionById(@PathVariable("id") Integer id) throws Exception {
        Question question = questionMapper.selectById(id);


        return Result.success(question);

    }
    /* @Description: 编辑问题,@Param:  */
    @PutMapping("/editQ")
    public Result editQuestion(@Validated @RequestBody Question question) throws Exception {
        boolean update = questionService.saveOrUpdate(question);
        if (update !=false) {

            System.out.println("更改成功");
            return Result.success(200, "success", question);
        } else {
            return Result.fail("更改失败");
        }
    }


    /* @Description: 删除问题,@Param:  */
    @DeleteMapping("/deleteQ/{id}")
    public Result deleteQuestion(@PathVariable("id") Integer id){
        int i = questionMapper.deleteById(id);
        System.out.println(i);
        if (i == 1){
            return Result.success("success");
        }else {
            return Result.fail("删除失败");
        }
    }

    /* @Description: 问题过审,@Param:  */
    /* @Description: 过审,@Param:  */
    @PutMapping("/pastQ/{id}")
    public Result pastQuestion(@PathVariable("id") Integer id){
        Question question = questionMapper.selectById(id);
        question.setIspast(1);
        questionService.saveOrUpdate(question);
        return Result.success("success");
    }
    /* @Description: 不过审,@Param:  */
    @PutMapping("/failQ/{id}")
    public Result failQuestion(@PathVariable("id") Integer id){
        Question question = questionMapper.selectById(id);
        question.setIspast(0);
        questionService.saveOrUpdate(question);
        return Result.success("success");
    }

    /* @Description: 回答过审,@Param:  */
    /* @Description: 过审,@Param:  */
    @PutMapping("/pastA/{id}")
    public Result pastAnswer(@PathVariable("id") Integer id){
        Answer answer = answerMapper.selectById(id);
        answer.setIspast(1);
        answerService.saveOrUpdate(answer);
        return Result.success("success");
    }
    /* @Description: 不过审,@Param:  */
    @PutMapping("/failA/{id}")
    public Result failAnswer(@PathVariable("id") Integer id){
        Answer answer = answerMapper.selectById(id);
        answer.setIspast(0);
        answerService.saveOrUpdate(answer);
        return Result.success("success");
    }


    /* @Description: 添加回答,@Param:  */
    @PostMapping("/addA")
    public Result addA(@Validated @RequestBody(required = false) Answer answer) throws Exception {

        int insert = answerMapper.insert(answer);
        if (insert != 0) {

            System.out.println("添加成功");
            return Result.success(200, "success", insert);
        } else {
            return Result.fail("添加失败");
        }
    }
    /* @Description: 根据问题id查找对应的回答,@Param:  */
    @GetMapping("/findAbyid/{qid}")
    public Result getAnswerById(@PathVariable("qid") Integer qid) throws Exception {
        List<Answer> answers = answerMapper.selectByQId(qid);

        return Result.success(answers);

    }
    /* @Description: 根据用户名查找对应的回答,@Pauram:  */
    @GetMapping("/findAbyuid/{uuid}")
    public Result getAnswerByName(@PathVariable("uuid") String uuid) throws Exception {
        List<Answer> answers = answerMapper.selectByuserId(uuid);


        return Result.success(answers);

    }
    /* @Description: 根据用户id查找对应的问题,@Param:  */
    @GetMapping("/findQbyuid/{uuid}")
    public Result getQuestionByName(@PathVariable("uuid") String uuid) throws Exception {
        List<Question> questions = questionMapper.selectByuserId(uuid);


        return Result.success(questions);

    }
    /* @Description: 编辑回答,@Param:  */
    @PutMapping("/editA")
    public Result editAnswer(@Validated @RequestBody Answer answer) throws Exception {
        boolean update = answerService.saveOrUpdate(answer);
        if (update !=false) {

            System.out.println("更改成功");
            return Result.success(200, "更改成功", answer);
        } else {
            return Result.fail("更改失败");
        }
    }

    /* @Description: 删除回答,@Param:  */
    @DeleteMapping("/deleteA/{id}")
    public Result deleteAnswer(@PathVariable("id") Integer id){
        int i = answerMapper.deleteById(id);
        if (i == 1){
            return Result.success("success");
        }else {
            return Result.fail("删除失败");
        }
    }
}
